﻿/*
=======================================================
    User【用户】
=======================================================
*/

using Entities;

namespace Services
{
    public interface IUserRepository : IRepository<User>
    {
        int UpdateIsDelete(bool flag, int[] ids);
        List<User> GetUserByLogin(string code, string loginName, string password);
        void ResetPassword(List<int> ids, string pwd);
        List<User> GetUserListByDepartmentID(int departmentID);
        bool GetLoginNameExists(string loginName);
    }


    public class UserRepository : BaseRepository<User>, IUserRepository
    {
        public UserRepository(ISqlSugarClient dB, ICurrentUser currentUser, IQuery<User> query)
            : base(dB, currentUser, query)
        {
        }

        #region 启用/禁用  UpdateIsDelete
        public int UpdateIsDelete(bool flag, int[] ids)
        {
            return _DB.Updateable<User>()
                      .SetColumns(x => new User { IsDelete = flag })
                      .Where(x => ids.Contains(x.UserID))
                      .ExecuteCommand();
        }
        #endregion

        #region GetUserByLogin
        public List<User> GetUserByLogin(string code, string loginName, string password)
        {
            password = Gx.DesEncrypt(password);
            string sql = string.Empty;

            sql += $" SELECT 1 AS LoginUserType,u.UserID,u.Name,u.GroupInfoID AS LoginTypeID FROM dbo.[User] u INNER JOIN glo.GroupInfo gi ON gi.GroupInfoID = u.GroupInfoID WHERE gi.Code = '{code}' AND gi.IsDelete = 0 AND u.LoginName = '{loginName}' AND u.LoginPassword = '{password}' AND u.IsDelete = 0 ";
            sql += $" UNION ALL ";
            sql += $" SELECT 2 AS LoginUserType,ru.RegulatorUserID AS [UserID],ru.Name,r.RegulatorID AS LoginTypeID FROM glo.RegulatorUser ru INNER JOIN glo.Regulator r ON r.RegulatorID = ru.RegulatorID WHERE r.Code = '{code}' AND r.IsDelete = 0 AND ru.LoginName = '{loginName}' AND ru.LoginPassword = '{password}' AND ru.IsDelete = 0 ";

            return _DB.Ado.SqlQuery<User>(sql).ToList();
        }
        #endregion

        #region Insert
        public override int Insert(User entity)
        {
            int userID = base.Insert(entity);

            if (entity.RoleIDs.IsNotEmpty() && entity.RoleIDs.Count > 0)
            {
                var userRoles = entity.RoleIDs.Select(x => new UserRole
                {
                    GroupInfoID = _currentUser.CurrentUserInfo.LoginTypeID,
                    UserID = userID,
                    RoleID = x,
                }).ToList();

                _DB.Insertable(userRoles).ExecuteCommand();
            }

            return userID;
        }
        #endregion

        #region Update
        public override int Update(User entity)
        {
            int n = base.Update(entity);

            _DB.Deleteable<UserRole>().Where(x => x.UserID == entity.UserID).ExecuteCommand();
            if (entity.RoleIDs.IsNotEmpty() && entity.RoleIDs.Count > 0)
            {
                var userRoles = entity.RoleIDs.Select(x => new UserRole
                {
                    GroupInfoID = _currentUser.CurrentUserInfo.LoginTypeID,
                    UserID = entity.UserID,
                    RoleID = x,
                }).ToList();

                _DB.Insertable(userRoles).ExecuteCommand();
            }

            return n;
        }
        #endregion

        #region GetByID
        public override User GetByID(int id)
        {
            var entity = base.GetByID(id);
            entity.RoleIDs = _DB.Queryable<UserRole>().Where(x => x.UserID == id).Select(x => x.RoleID).ToList();
            return entity;
        }
        #endregion

        #region 重置密码  ResetPassword
        public void ResetPassword(List<int> ids, string pwd)
        {
            _DB.Updateable<User>().SetColumns(x => x.LoginPassword == pwd)
                .Where(x => ids.Contains(x.UserID))
                .ExecuteCommand();
        }
        #endregion

        #region 获取部门中的用户列表  GetUserListByDepartmentID
        public List<User> GetUserListByDepartmentID(int departmentID)
        {
            return _DB.Queryable<User>()
                .InnerJoin<DepartmentUser>((u, du) => u.UserID == du.UserID)
                .Where((u, du) => du.DepartmentID == departmentID)
                .Select((u, du) => new User { UserID = u.UserID.SelectAll(), DepartmentUserID = du.DepartmentUserID })
                .ToList();
        }
        #endregion

        #region GetLoginNameExists
        public bool GetLoginNameExists(string loginName)
        {
            return _query.GetQueryable().Where(x => x.LoginName == loginName).Count() > 0;
        }
        #endregion
    }
}
